<?php
/**
 * Created by PhpStorm.
 * User: dllo
 * Date: 16/8/30
 * Time: 下午7:49
 */

//$a=rand(1,13);
//echo $a;

//提交姓名和电话
//api:shuqian_api.php?type=submit&name=xx&tel=xx&openid=xx
//返回值:{err:1,msg:"失败原因"}

//保存分数
//api:shuqian_api.php?type=score&openid=xx&score=100
//返回值:{err:1,msg:"网络错误",rank:10}


$type = $_GET["type"];
mysql_connect(SAE_MYSQL_HOST_M.":".SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
mysql_select_db(SAE_MYSQL_DB);
mysql_query("set names utf8");

//后台随机奖品编码
$prizenum = rand(0,11);


switch ($type){
    case "randomPrize":
        $openid = $_GET["openid"];
        $sql = "UPDATE madcar SET prizenum='{$prizenum}' WHERE openid='{$openid}'";
        mysql_query($sql);
        if(mysql_affected_rows()>0){
            $arr = array("err"=>0,"msg"=>"成功","prizenum"=>$prizenum);
            echo json_encode($arr);
        }else{
            echo json_encode(array("err"=>1,"openid"=>$openid));
            //echo '{"err":1,"msg":"更新失败,请检查输入的内容"}';
        }
        break;
    case "register":
        $openid = $_GET["openid"];
        $username = $_GET["username"];
        $tel = $_GET["tel"];
        $province = $_GET["province"];
        $city = $_GET["city"];
        $address = $_GET["address"];
        $sql = "UPDATE madcar SET username='{$username}',tel='{$tel}',province='{$province}',city='{$city}',address='{$address}' WHERE openid='{$openid}'";
        mysql_query($sql);
        if(mysql_affected_rows()>0){
            $arr = array("err"=>0,"msg"=>"成功","username"=>$username,"tel"=>$tel,"province"=>$province,"city"=>$city,"address"=>$address);
            echo json_encode($arr);
        }else{
            echo '{"err":1,"msg":"更新失败,请检查输入的内容"}';
        }
        break;

    //超过全国xx%玩家
    //有人点晋级
    case "up":
        $openid = $_GET["openid"];
        $up = $_GET["up"];
        $sql = "UPDATE madcar SET up='{$up}' WHERE openid='{$openid}'";
        $result = mysql_query($sql);
        if(mysql_num_rows($result)>0) {
            $arr = array("err"=>0,"msg"=>"点赞成功","up"=>$up);
            echo json_encode($arr);
        }
        break;

    //有人点击你呀太差
    case "down":
        $openid=$_GET["openid"];
        $down = $_GET["down"];
        $sql = "UPDATE madcar SET down='{$down}' WHERE openid='{$openid}'";
        $result = mysql_query($sql);
        if(mysql_num_rows($result)>0){
            $arr = array("err"=>0,"msg"=>"点你丫太差成功","down"=>$down);
            echo json_encode($arr);
        }
        break;

    //car.php页面获取,判断晋级与否?
    case "gotup":
        $openid = $_GET["openid"];
        $up = $_GET["up"];
        $sql = "SELECT * FROM madcar";
        $result = mysql_query($sql);
        if(mysql_num_rows($result)>0) {
            $arr = array("err"=>0,"msg"=>"点赞成功","up"=>$up,"nickname"=>$nickname);
            echo json_encode($arr);
        }
        break;

    case "gotdown":
        $openid = $_GET["openid"];
        $up = $_GET["down"];
        $sql = "SELECT * FROM madcar";
        $result = mysql_query($sql);
        if(mysql_num_rows($result)>0) {
            $arr = array("err"=>0,"msg"=>"点赞成功","down"=>$down);
            echo json_encode($arr);
        }
        break;
    case "number":
        $openid = $_GET["openid"];
        $score=$_GET["score"];
        $sql  = "UPDATE madcar SET score='{$score}' WHERE openid='{$openid}'";
        mysql_query($sql);
        if(mysql_affected_rows()>0){
            //保存分数成功,下一步查找分数排名
            $sql = "SELECT COUNT(id) FROM madcar WHERE score>{$score}";
            $result1 =  mysql_query($sql);
            $row1 = mysql_fetch_row($result1);
            $rank1 = $row1[0];
            //$arr1 = array("err"=>0,"msg"=>"成功","rank1"=>$rank1);
            //数组转成json结构字符串
            //echo json_encode($arr1);
            $sql = "SELECT COUNT(id) FROM madcar";
            $result2 =  mysql_query($sql);
            $row2 = mysql_fetch_row($result2);
            $rank2 = $row2[0];
            $rank3=(($rank2-$rank1)/$rank2*100)."%";
            $arr2 = array("err"=>0,"msg"=>"成功","rank3"=>$rank3);
            //数组转成json结构字符串
            echo json_encode($arr2);
        }else{
            echo '{"err":1,"msg":"分数更新失败"}';
        }
        break;
    case "paihang":
        $sql = "SELECT * FROM madcar WHERE ORDER BY RSORT(score) LIMIT 8";
        $result=mysql_query($sql);
        $arr=[];
        while($row=mysql_fetch_row($result)){
            array_push($arr,$row);
        }
        echo json_encode($arr);
        break;
}
?>







